package stage1.operation.module4.login;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端
 */
public class Client {
    public static void main(String[] args) {
        String serverName = "127.0.0.1";
        int port = 6066;
        try {
            System.out.println("连接到主机：" + serverName + " ，端口号：" + port);
            Socket client = new Socket(serverName, port);
            System.out.println("远程主机地址：" + client.getRemoteSocketAddress());
            OutputStream outToServer = client.getOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(outToServer);

            User user = new User("admin", "123456");
            UserMessage userMessage = new UserMessage("", user);
            out.writeObject(userMessage);
            out.flush();
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
            Object object = in.readObject();
            UserMessage userMessageN = (UserMessage)object;
            if ("success".equalsIgnoreCase(userMessageN.getType()))
            {
                System.out.println("登录成功！");
            }else {
                System.out.println("登录失败！");
            }
            client.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
